home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR330.ZIP / SRC / M.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-05  |  8.8 KB  |  358 lines

  1. #include "wand_head.h"
  2. #include <sys/time.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <curses.h>
  7.  
  8. #ifdef TOS
  9. #include <keycodes.h>
  10. extern long _stksize = 50000L;
  11. #endif
  12.  
  13. extern char *playscreen();
  14. extern int rscreen();
  15. extern int savescore();
  16. extern void map();
  17. extern void check_legality();
  18. int err;
  19. int debug_disp = 0;
  20. char screen[NOOFROWS][ROWLEN+1];
  21. int edit_mode = 0;
  22. int saved_game = 0;
  23. char *edit_screen = (char *) 0;
  24. char *edit_memory = (char *) 0;
  25. char *memory_end = (char *) 0;
  26. char screen_name[61] ;
  27. int record_file = -1;
  28. long dictsize = 0L;
  29. FILE *dictfp;
  30. WINDOW *win,*mapwin,*displaywin,*infowin;
  31. WINDOW *messagewin;
  32. int old_score=0;
  33. int pause1 = 30; /* delay time for moving objects */
  34. int pause2 = 50; /* delay time for playback */
  35. int recording=0; /* need to link it also with fall.c */
  36.  
  37. int readline(fd, ptr, max)
  38. int fd, max;
  39. char *ptr;
  40. {
  41.     int count = 0;
  42.     while (read(fd,ptr,1) == 1) {
  43.     if (++count == max)
  44.         break;
  45.     if (*ptr=='\n')
  46.         break;
  47.     ptr++;
  48.     }
  49.     *ptr = '\0';
  50.     return count;
  51. }
  52.  
  53. show_credits(opt)
  54. int opt;
  55. {
  56.     int maxlines, linecount;
  57.     FILE *fp;
  58.     char ch, buffer[100];
  59. #ifdef TOS
  60.     long i;    /* index used to approximate half second between scrolls */
  61. #else
  62.     int inp, nul;
  63.     struct timeval tv;
  64. #endif
  65.  
  66.     sprintf(buffer,"%s/credits",SCREENPATH);
  67.     if ((fp = fopen(buffer,"r")) == NULL) {
  68.     printf("Sorry - credits unavailable!\n");
  69.     exit(1);
  70.     }
  71.     initscr();
  72.     win = stdscr;
  73.     CBON;
  74.     noecho();
  75.     maxlines = /* tgetnum("li") - 3;*/ 21;
  76.     linecount = 0;
  77. #ifndef TOS
  78.     nul = 0;
  79.     tv.tv_sec = 0;
  80.     tv.tv_usec = 500000L;    /* half second between scrolls */
  81. #endif
  82.     while (fgets(buffer,100,fp)) {
  83.     addstr(buffer);
  84.     if (!opt) {
  85.         if (++linecount == maxlines) {
  86.         move(maxlines + 2,0);
  87.         addstr("-- More --");
  88.         refresh();
  89.         ch = getch();
  90.         if (ch == 'q') break;
  91.         move(maxlines + 2,0);
  92.         addstr("          ");
  93.         refresh();
  94.         clear();
  95.         linecount = 0;
  96.         }
  97.     } else {
  98. #ifdef TOS
  99.         printf("%s",buffer);
  100.         for (i = 0; i < 110000; i++);
  101.         if (console_input_status(0)) {    /* if character is available */
  102.         read(0,&ch,1);
  103.         if (ch == 'q') break;
  104.         }
  105. #else
  106.         inp = 1;
  107.         printf("%s",buffer);
  108.         /* select(1,&inp,&nul,&nul,&tv); SS compiler in compatibility */
  109.         if (inp) {
  110.         read(0,&ch,1);
  111.         if (ch == 'q') break;
  112.         }
  113. #endif
  114.     }
  115.     }
  116.     CBOFF;
  117.     echo();
  118.     if (!opt) {
  119.     move(maxlines+2,0);
  120.     refresh();
  121.     }
  122.     endwin();
  123. }
  124.  
  125. extern int optind, opterr;
  126. extern char *optarg;
  127.  
  128. main(argc, argv)
  129. int  argc;
  130. char **argv;
  131. {
  132.     char (*frow)[ROWLEN+1] = screen;
  133.     long score = 0;
  134.     int fp, num = 1, bell = 0, maxmoves = 0, x, y;
  135.     char howdead[25], buffer[100], *name, *keys, *dead;
  136.     char c, *malloc();
  137.     struct stat statbuf;
  138.  
  139.     keys = (char *) 0;
  140.     memory_end = edit_memory = malloc(EMSIZE * sizeof(char));
  141.     while ((c = getopt(argc,argv,"01k:et:r:fmCcvs")) != -1)
  142.     switch (c) {
  143.     case '0': bell = 0; break;
  144.     case '1': bell = 1; break;
  145.     case 'k': keys = optarg; break;
  146.     case 'm': erase_scores();
  147.         exit(0);
  148.     case 'c': show_credits(0);
  149.         exit(0);
  150.     case 'C': show_credits(1);
  151.         exit(0);
  152.     case 's': savescore("-",-1L,-1,"-");
  153.         exit(0);
  154.     case 'f': debug_disp = 1; break;
  155.     case 'v':
  156.          printf("\nWANDERER Copyright (C) 1988 S. Shipway. Version %s.\n\n",VERSION);
  157.       printf("Adapted for MSDOS in protected mode (using DJGPP).\n");
  158.          printf("by seymour.shlien@crc.doc.ca\n");
  159.       exit (0);
  160.  
  161.     case 'e': edit_mode = 1;
  162.         break;
  163.  
  164.     case 't': edit_screen = optarg;
  165.         debug_disp = edit_mode = 1;
  166.         rscreen(0,&maxmoves);
  167.         initscr();
  168.             win = stdscr;
  169.         map(frow);
  170.         check_legality();
  171.         move(21,0); refresh();
  172.         endwin();
  173.         exit(0);
  174.     default:
  175.         fprintf(stderr,"Usage: %s [ -e | -m | -C | -c | -s | [ -f ] | -t file ] [ -0 | -1 ] [ -k keys ] [ file ]\n",argv[0]);
  176.         exit(1);
  177.     }
  178.  
  179.     if (optind < argc) edit_screen = argv[optind];
  180.  
  181. /* bjr@watserv1.waterloo.edu, added the following to fix password searching.
  182.    To properly search the 'words' dictionary, the size of the file
  183.    is needed.  As a result, there are now five conditions (there were
  184.    originally two,) under which passwords will be disabled.
  185.     1. The program was run with flag -e (screen editor)
  186.     2. The file no_pws is present in the screens directory
  187.     3. A stat() call on the dictionary file fails
  188.     4. The dictionary file cannot be opened using fopen()
  189.     5. The dictionary is empty (0 bytes)
  190.    The variable no_passwords has been replaced by dictsize.  If
  191.    dictsize = 0, passwords are disabled. */
  192.  
  193.     if (!edit_mode) {
  194.     sprintf(buffer,"%s/no_pws",SCREENPATH);
  195. #ifdef TOS
  196.     if ((fp = open(buffer,O_RDONLY)) < 0)    /* if no_pws does not exist */
  197. #else
  198.     if ((fp = open(buffer,O_RDONLY)) == -1)    /* if no_pws does not exist */
  199. #endif
  200.      {err = stat(DICTIONARY,&statbuf);
  201.       if (err == 0)    /* and we can stat dictionary */
  202.         if ((dictfp = fopen(DICTIONARY, "r")) != NULL) /* and open */
  203.             dictsize = statbuf.st_size; /* save size */
  204.       
  205.      close(fp);
  206.          }
  207.     }
  208.  
  209.     if ((name = (char *)getenv("NEWNAME")) == NULL)
  210.     if ((name = (char *)getenv("NAME")) == NULL)
  211.         if ((name = (char *)getenv("FULLNAME")) == NULL)
  212.         if ((name = (char *)getenv("USER")) == NULL)
  213.             if ((name = (char *)getenv("LOGNAME")) == NULL)
  214. #ifdef    ASKNAME    /* M001 */
  215.             {
  216.             name = malloc(80L);
  217.             if (name == NULL) {
  218.                 printf("malloc error\n");
  219.                 exit(1);
  220.             }
  221.             printf("Name? "); fflush(stdout);
  222.             scanf("%s",name);
  223.             if (name[0] == '\0')
  224.                 name = "noname";
  225.             }
  226. #else
  227.             name = "noname";
  228. #endif
  229.  
  230.     if (!keys)
  231.     if ((keys = (char *)getenv("NEWKEYS")) == NULL) {
  232.         keys = malloc(5);
  233.          strcpy(keys,"kjhl");        
  234.     }
  235.  
  236. #ifdef TOS
  237. /* bind the cursor keys to the movement keys.  Also, bind */
  238. /* the HELP key to the '?' command. */
  239.     {
  240.     char key[2] = "?";
  241.     key[0] = keys[0]; console_set_key(CURS_UP, key, NULL, NULL);
  242.     key[0] = keys[1]; console_set_key(CURS_DN, key, NULL, NULL);
  243.     key[0] = keys[2]; console_set_key(CURS_LF, key, NULL, NULL);
  244.     key[0] = keys[3]; console_set_key(CURS_RT, key, NULL, NULL);
  245.     console_set_key(K_HELP, "?", NULL, NULL);
  246.     }
  247. #endif
  248.  
  249.     initscr();
  250.     win = stdscr;
  251.     displaywin = stdscr;
  252.     mapwin = stdscr;
  253.  
  254.     start_color();
  255.     infowin = newwin(19,32,0,45);
  256.  
  257.     displaywin = newwin(17,36,0,0);
  258.     init_pair(1,COLOR_WHITE,COLOR_BLACK);
  259.     init_pair(2,COLOR_RED,COLOR_WHITE);
  260.     wbkgd(displaywin,COLOR_PAIR(2)); 
  261.     err=keypad(displaywin,1); /* enable function/arrow keys */
  262.  
  263.     mapwin = newwin(18,42,0,0);
  264.     init_pair(3,COLOR_BLUE,COLOR_YELLOW);
  265.     wbkgd(mapwin,COLOR_PAIR(2));
  266.     err=keypad(mapwin,3); /* enable function/arrow keys */
  267.  
  268.     messagewin = newwin(5,70,20,0);
  269.  
  270.     if (!edit_mode)
  271.       win = mapwin;
  272.  
  273.  
  274. /* MAIN PROGRAM HERE */
  275.  
  276.     CBON; noecho();
  277.  
  278.     if (!edit_mode) {
  279.     for (;;) {
  280.         if (rscreen(num,&maxmoves)) {
  281.         strcpy(howdead,"a non-existant screen");
  282.         break;
  283.         }
  284.         dead = playscreen(&num,&score,&bell,maxmoves,keys);
  285.         if ((dead != NULL) && (*dead == '~')) {
  286.         num = (int)(dead[1]) - 1;
  287.         dead = NULL;
  288.         }
  289.         if (dead != NULL) {
  290.         strcpy(howdead,dead);
  291. #ifdef FRIENDLY
  292.         if(retry(name,howdead)==1) break;
  293.         else num--;
  294. #else
  295.         break;
  296. #endif
  297.         }
  298.         num++;
  299.     }
  300.     } else {
  301.     if (rscreen(num,&maxmoves)) {
  302.         for (x = 0; x < ROWLEN; x++)
  303.         for (y = 0; y < NOOFROWS; y++)
  304.             screen[y][x] =  ' ';
  305.     }
  306.     editscreen(num,&score,&bell,maxmoves,keys);
  307.     }
  308. /* END OF MAIN PROGRAM */
  309.  
  310. /* SAVE ROUTINES FOR SCORES */
  311.  
  312.     if (!edit_mode) {
  313.     if ((savescore(howdead,score,num,name) == 0) && (score != 0))
  314.         printf("\nWARNING: %s : score not saved!\n",argv[0]);
  315.     }
  316.  
  317.     echo();
  318.     CBOFF;
  319.     endwin();
  320.     printf("WANDERER (C) 1988 S. Shipway. DJGPP MSDOS version by S.Shlien (1997).\n");
  321.     if (record_file != -1) close(record_file);
  322.     if (!dictsize) fclose(dictfp);    /* bjr */
  323. #ifdef TOS
  324.     if (!strcmp(argv[0], "")) {        /* probably run from the desktop */
  325.     printf("Press any key to end...");
  326.     c = getch();
  327.     }
  328. #endif    /* TOS */
  329. }
  330.  
  331. int retry(name,howdead,score)
  332. char *name,howdead[];
  333. int *score;
  334. {    char ans;
  335.     wclear(messagewin);
  336.     wattron(messagewin,A_BOLD);
  337.         wmove(messagewin,0,0);
  338.     waddstr(messagewin,name);
  339.         waddstr(messagewin," killed by ");
  340.         waddstr(messagewin,howdead);
  341.     wattroff(messagewin,A_BOLD);
  342.         wmove(messagewin,1,0);
  343.     ans = ' ';
  344.     waddstr(messagewin,"Do you want to exit?");
  345.     wrefresh(messagewin);
  346.     ans = wgetch(messagewin);
  347.     while (ans !='n' && ans != 'y')
  348.         {
  349.         waddstr(messagewin,"Enter y or n :");
  350.         ans = wgetch(messagewin);
  351.         }
  352.     wclear(messagewin);
  353.     wrefresh(messagewin);
  354.         if (ans == 'y') return 1;
  355.     else {*score = old_score; return 0;}
  356. }
  357.     
  358.